package servlet;

import dao.LabelDao;
import dao.daoimpl.LabelDaoImpl;
import bean.Label;
import bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name="UpdateLabelServlet",urlPatterns = "/UpdateLabelServlet")
public class UpdateLabelServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");/*防止乱码*/
        User user=(User )request.getSession().getAttribute("user");
        int labelId=Integer.parseInt(request.getParameter("updateArticleLabelId"));/*获取标签名字*/
        String content=request.getParameter("updateArticleLabelContent");/*获取标签内容*/
        String time= new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());/*获取当前时间*/
        LabelDao labelDao=new LabelDaoImpl();
        if(labelDao.updateLabel(new Label(labelId,content,time))){
            System.out.println("用户修改标签成功");
            try {
                request.getSession().setAttribute("userLabels",labelDao.getUserLabels(user));
            } catch (SQLException sqlException) {
                sqlException.printStackTrace();
            }
        }else{
            System.out.println("用户修改标签失败");
        }
    }
}
